using Google.Protobuf.Reflection;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.Extensions.DependencyInjection;
using Models;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using OrleansDemo.Grain;
using System.Net;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var masterServer = builder.Configuration.GetSection("MasterServer").Get<ServerItem>();

builder.Host.UseOrleansClient(c =>
{
    //c.UseLocalhostClustering(new int[] { 30001, 30002, 30003 }, "HelloWorldApp", "dev")
    c.UseStaticClustering(new IPEndPoint(IPAddress.Parse(masterServer.ServerIp), masterServer.GatewayPort))
    .Configure<ClusterOptions>(options =>
    {
        options.ClusterId = "dev";
        options.ServiceId = "HelloWorldApp";
    })
    .Configure<Orleans.Configuration.ClientMessagingOptions>(options =>
    {
        options.ResponseTimeout = TimeSpan.FromMinutes(5);
    })
    .ConfigureServices(serviceCollection =>
    {
        serviceCollection.AddScoped(typeof(IHello), typeof(HelloGrain));
        serviceCollection.AddScoped(typeof(IHello2), typeof(HelloGrain2));
    })
    //.UseAdoNetClustering(option =>
    //{
    //    option.ConnectionString = "server=localhost;database=orleans_demo;user id=root;password=123456;character set=utf8mb4;";
    //    option.Invariant = "MySql.Data.MySqlClient";
    //})
    ;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();
app.MapControllers();

app.Run();
